package org.ioc.com.book_test1.controller;

import jakarta.servlet.http.HttpSession;
import org.ioc.com.book_test1.constant.Constants;
import org.ioc.com.book_test1.model.UserInfo;
import org.ioc.com.book_test1.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @RequestMapping("/login")
    public String userLogin(String userName, String password, HttpSession session){
        //1.参数校验
        if (!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return "用户名或密码为空";
        }
        //2.通过数据库的用户数据和传入的参数进行匹配
        UserInfo user=userInfoService.selectByName(userName);
        if (user==null){
            //证明这个用户不存在
            return "用户不存在";
        }
        if (!password.equals(user.getPassword())){
            return "账号或密码错误";
        }
        session.setAttribute(Constants.USER_SESSION_KEY,user);
        return "";
    }
}
